package com.cowain.monitor.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cowain.monitor.dao.ErpClientDao;
import com.cowain.monitor.dao.PmsClientDao;
import com.cowain.monitor.datasources.DataSourceNames;
import com.cowain.monitor.datasources.annotation.DataSource;
import com.cowain.monitor.datasources.config.TransactionConfig;
import com.cowain.monitor.entity.ErpClientEntity;
import com.cowain.monitor.entity.ErpSoEntity;
import com.cowain.monitor.entity.PmsClientEntity;
import com.cowain.monitor.entity.PmsOrderInfoEntity;
import com.cowain.monitor.service.ErpClientService;
import com.cowain.monitor.utils.PmsConstantUtils;
import com.cowain.monitor.utils.PmsProjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  
 *  * @author 冬暖
 *  * @projectName pms-monitor
 *  * @title     ErpClientServiceImpl   
 *  * @package    com.cowain.monitor.service.impl  
 *  * @description    ERP客户信息       
 *  * @date   2020/7/28 18:00  
 *  * @version V1.0.0
 *  
 */
@Service
public class ErpClientServiceImpl extends ServiceImpl<ErpClientDao,ErpClientEntity> implements ErpClientService {
    @Autowired
    private PmsClientDao clientDao;
    @Autowired
    private PmsProjectUtil pmsProjectUtil;
    /**
     * 获取所有客户信息
     * @return 客户列表
     */
    @Override
    @DataSource(name = DataSourceNames.SECOND)
    public List<ErpClientEntity> findClientList(){
        return baseMapper.findClientList(PmsConstantUtils.IS_DEL);
    }

    /**
     * 添加客户
     */
    @Override
    @DataSource(name = DataSourceNames.FIRST)
    @Transactional(value = TransactionConfig.DEFAULT_TX,rollbackFor = Exception.class)
    public void insertClient(List<ErpClientEntity> clientEntityList){
        List<PmsClientEntity> infoList = new ArrayList<>();
        List<PmsClientEntity> infoEntityList = new ArrayList<>();
        clientEntityList.forEach(client -> {
            Long clientNumber = clientDao.findClient(client.getClientNumber());
            PmsClientEntity clientEntity = new PmsClientEntity();
            clientEntity.setClientName(client.getClientName());
            clientEntity.setClientAddress(client.getClientAddress());
            clientEntity.setClientType(client.getClientType());
            clientEntity.setClientContact(client.getClientContact());
            clientEntity.setContactPhone(client.getClientPhone());
            clientEntity.setClientIntension(null);
            clientEntity.setId(client.getClientNumber());
            clientEntity.setReference(null);
            clientEntity.setInsertTime(new Date());
            clientEntity.setIndustry(client.getIndustry());
            clientEntity.setInsertUser(null);
            if(clientNumber != null){
                infoEntityList.add(clientEntity);
            }else {
                infoList.add(clientEntity);
            }
        });
        List<PmsClientEntity> list1 = new ArrayList<>();
        List<Long> list = pmsProjectUtil.addClientList(infoList, list1);
        if(infoList.size() > 0 && list.size() > 0){
            List<PmsClientEntity> list2 = new ArrayList<>();
            infoList.forEach(info -> {
                if(info.getId() == null){
                    list2.add(info);
                }
            });
            infoList.removeAll(list2);
            if(infoList.size() > 0){
                List<List<PmsClientEntity>> split = pmsProjectUtil.split(infoList, 12);
                for (List<PmsClientEntity> clientEntities : split) {
                    clientDao.insertList(clientEntities);
                }

            }
        }
        if(infoEntityList.size() > 0){
            if(list1.size() > 0){
                infoEntityList.addAll(list1);
            }
            List<List<PmsClientEntity>> split = pmsProjectUtil.split(infoEntityList, 12);
            for (List<PmsClientEntity> pmsClientEntities : split) {
                clientDao.updateInfoList(pmsClientEntities);
            }
        }
        if(list1.size() ==0){
            list1.addAll(infoEntityList);
        }
        if(list.size() > 0 || list1.size() > 0) {
            pmsProjectUtil.addLog(infoList, list1, PmsConstantUtils.ERP_USER, PmsConstantUtils.ERP_USER);
        }
    }

    /**
     * 修改客户状态
     */
    @Override
    @DataSource(name = DataSourceNames.SECOND)
    @Transactional(value = TransactionConfig.SECOND_TX,rollbackFor = Exception.class)
    public void updateClient(List<ErpClientEntity> clientEntityList){
        clientEntityList.forEach(list -> {
            if(list.getClientNumber() != null){
                list.setIsDel(1);
            }else {
                list.setIsDel(0);
            }
        });
        List<ErpClientEntity> collect = clientEntityList.stream()
                .filter(s -> s.getIsDel() == 1)
                .collect(Collectors.toList());
        List<List<ErpClientEntity>> split = pmsProjectUtil.split(collect, 12);
        for (List<ErpClientEntity> erpClientEntities : split) {
            baseMapper.updateClient(erpClientEntities);
        }
    }
}
